<?php
// $Id$

/**
 * Controller_Admin_module 控制器
 */
class Controller_Admin_tabbar extends Controller_Abstract
{
	function actionIndex()
	{
		$append = array('acct_id'=>$_SESSION['acct']['id']);
		$this->union_table = tabbar::meta();
		$this->_context->orderField = 'sequence';
		$this->_context->orderDirection = 'asc';
		$this->lists($append);
		
	}

    function actionadd()
    {
        $this->union_table = tabbar::meta();
        $handle = new helper_autoinsert($this->union_table);
        if($this->_context->isPOST()){
            $sequence=intval($this->_context->sequence);
            //if($sequence>7 || $sequence<1)  $this->error('排序只能为1到7之间的整数');
        }
        $form = new Form_module_add(url($this->_context->controller_name.'/'.$this->_context->action_name));
        $result = $handle->add($form, 'id',$_GET);
       
        if ($this->_context->isGET())
        {
            $this->_view['form'] = $result;
            $this->_viewname = '../form/add';
        }else {
            Syslog::logs("编辑首页中间菜单名称:".$_POST['name']);
            if($handle->status=='success')
                $this->_context->forwardUrl = url("module/index");
            return call_user_func_array(array($this,$handle->status),array($result));
        }
    }

    function actiondel()
    {
        Syslog::logs("删除首页中间菜单，ID：".print_r($this->_context->ids,true));
        $personalmodule=personalmodule::find(array('id'=>$this->_context->ids))->getOne();
  
        
        personalmodule::meta()->deleteWhere(array_merge(array('id'=>$this->_context->ids,'acct_id'=>$_SESSION['acct']['id'])));
        $this->success('删除成功');
    }
    /**
     * 添加、修改首页中间菜单
     */
    function actionNewAdd()
    {

        $id = isset( $this->_context->id ) ? intval($this->_context->id) : 0;

        $acct_id = intval($this->_context->acct_id);

        //$mz_acctid = intval($_REQUEST['mz_acctid']);
        $module = trim($this->_context->moduleselect );

        $name = trim($this->_context->name );
        $img = trim( $this->_context->img );
        $act_img = trim( $this->_context->act_img );
        $url = trim( $this->_context->url );
        if($id){
            $db = tabbar::find('id = ?',$id)->getOne();
        }else{
            $db = new tabbar();
            $db->sequence = 100;
        }
        $db->acct_id = $acct_id;
        //$db->mz_acctid = $mz_acctid;
        $db->moduleselect = $module;
        if($module == 'url'){
            $db->url = $url;
        }else{
            $db->url = tabbar::getmoduleurl($module);
        }
        $db->name = $name;
        $db->img = $img;
        $db->act_img = $act_img;
        $db->save();
        if($id){
            Syslog::logs("修改TabBar，ID：".$id);
        }else{
            Syslog::logs("新增TabBar，ID：".$db->id);
        }
        return json_encode(array('id'=>$db->id, 'name'=>$db->name, 'img'=>$db->img, 'module'=>$module, 'url'=>$url));
    }
    /**
     * 删除首页中间菜单
     */
    function actionNewDel()
    {
        $id = intval($_REQUEST['id']);
        Syslog::logs("删除TabBar，ID：$id");
        //$personalmodule=tabbar::find(array('id'=>$id))->getOne();

        tabbar::meta()->deleteWhere(array_merge(array('id'=>$id,'acct_id'=>$_SESSION['acct']['id'])));
        return 'ok';
    }
	/**
	 * 首页中间菜单特效
	 */
	function  actionSpecial(){
        $module = tabbar::find('acct_id =?',$_SESSION['acct']['id'])->getOne();
        
        if(!$module->id()){
            tabbar::_inittabbars($_SESSION['acct']['id']);
        }
        $acct_id = $_SESSION['acct']['id'];
	    $this->_view['acct_id'] = $acct_id;

        $result = tabbar::find('acct_id = ? ', $acct_id)->order("sequence asc")->getAll();
        $this->_view['lists'] = $result;
    }
    /**
     * 拖拽首页中间菜单前
     */
    function  actionReordering(){

    }
    /**
     * 拖拽首页中间菜单后
     */
    function  actionReordered(){
        $arr = json_decode($_REQUEST['data']);
        //$arr = array_reverse($arr);
        $acct_id = $_SESSION['acct']['id'];
        foreach($arr as $key=>$id){
            $sql = "UPDATE tab_bar SET sequence = %d WHERE id = %d AND acct_id = %d";
            $sql = sprintf($sql,1+intval($key),intval($id),$acct_id);
            QDB::getConn()->execute($sql);
        }
 
        Syslog::logs("首页中间菜单新序列：".print_r($arr, true));
        return 'ok';
    }
}